package com.example.his.api.mis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.example.his.api.common.PageUtils;
import com.example.his.api.db.dao.RoleDao;
import com.example.his.api.db.pojo.RoleEntiy;
import com.example.his.api.mis.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


@Service
public class RoleServiceImpl implements RoleService {
  
  @Resource
  private RoleDao roleDao;
  
  @Override
  public ArrayList <HashMap> searchAllRole() {
    ArrayList <HashMap> list = roleDao.searchAllRole();
    return list;
  }
  
  @Override
  @Transactional
  public boolean insert(Map param) {
    RoleEntiy entityRole = BeanUtil.toBean(param, RoleEntiy.class);
    roleDao.insert(entityRole);
    Integer id = entityRole.getId();
    return id != null;
  }
  
  @Override
  public Integer searchByRoleName(String roleName) {
    Integer i = roleDao.searchByRoleName(roleName);
    return i;
  }
  
  @Override
  public HashMap searchById(Integer id) {
    HashMap user = roleDao.searchById(id);
    return user;
  }
  
  @Override
  @Transactional
  public boolean updateById(Map param) {
    int i = roleDao.updateById(param);
    return i == 1;
  }
  
  @Override
  @Transactional
  public int deleteByIds(ArrayList <Integer> ids) {
    int i = roleDao.deleteByIds(ids);
    return i;
  }
  
  @Override
  public PageUtils searchByPage(Map param) {
    ArrayList list = new ArrayList();
    long count = roleDao.searchCount(param);
    if(count > 0) {
      list = roleDao.searchByPage(param);
    }
    
    int page = MapUtil.getInt(param, "page");
    int length = MapUtil.getInt(param, "length");
    
    PageUtils pageUtils = new PageUtils(list, count, page, length);
    
    
    return pageUtils;
  }
}


